<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\server;

class Order
{
    public $_observer = [];
    public function __construct()
    {
    }
    public function memberDiscountData($list, $user_id, $is_remember = 1, $uniacid = 666)
    {
        $list["member_discount_balance"] = 0;
        $list["member_discount_cash"] = 0;
        $list["member_status"] = 0;
        $data = memberDiscountAuth($uniacid);
        if($data["status"] != 1) {
            return $list;
        }
        $user_model = new \app\massage\model\User();
        $member_discount_time = $user_model->where(["id" => $user_id])->value("member_discount_time");
        if($member_discount_time < time()) {
            return $list;
        }
        $list["member_status"] = 1;
        if($is_remember == 0) {
            return $list;
        }
        $total_goods_discount = $total_material_discount = 0;
        foreach ($list["order_goods"] as $k => $v) {
            $goods_discount = (10 - $data["discount"]) / 10 * $v["true_price"];
            $list["order_goods"][$k]["true_price"] = $v["true_price"] - $goods_discount;
            $material_discount = (10 - $data["discount"]) / 10 * $v["true_material_price"];
            $list["order_goods"][$k]["true_material_price"] = $v["true_material_price"] - $material_discount;
            $list["order_goods"][$k]["member_discount_cash"] = $material_discount + $goods_discount;
            $total_goods_discount += $goods_discount;
            $total_material_discount += $material_discount;
        }
        $list["member_discount_balance"] = $data["discount"];
        $list["member_discount_cash"] = round($total_goods_discount + $total_material_discount, 2);
        $list["goods_price"] = round($list["goods_price"] - $total_goods_discount, 2);
        $list["material_price"] = round($list["material_price"] - $total_material_discount, 2);
        $list["is_remember"] = 1;
        return $list;
    }
    public function upOrderMemberDiscountData($list, $order_id)
    {
        $order_data_model = new \app\massage\model\OrderData();
        $member_discount_balance = $order_data_model->where(["order_id" => $order_id])->value("member_discount_balance");
        $member_discount = 0 < $member_discount_balance ? $member_discount_balance : 10;
        $del_service_discount = $del_material_discount = 0;
        foreach ($list["list"] as $k => $v) {
            $goods_discount = (10 - $member_discount) / 10 * $v["now_init_service_price"];
            $material_discount = (10 - $member_discount) / 10 * $v["now_init_material_price"];
            $list["list"][$k]["member_discount_cash"] = $material_discount + $goods_discount;
            $list["list"][$k]["true_price"] -= $goods_discount;
            $list["list"][$k]["true_material_price"] -= $material_discount;
            $del_service_discount += $v["del_init_service_price"] * (10 - $member_discount) / 10;
            $del_material_discount += $v["del_init_material_price"] * (10 - $member_discount) / 10;
        }
        $list["del_total_discount"] = round($del_service_discount + $del_material_discount, 2);
        $list["member_discount_balance"] = round($member_discount_balance, 2);
        return $list;
    }
    public function UpOrderBalanceDiscountData($list, $user_id, $service_price, $balance_discount_card_arr = [])
    {
        $list["is_balancediscount"] = 0;
        $list["balance_discount_cash"] = $list["is_balancediscount"];
        $balance_card_model = new \app\balancediscount\model\UserCard();
        $card_list = $balance_card_model->getUserCard($user_id, $service_price, 0, $balance_discount_card_arr);
        $uniacid = 666;
        $data = balanceDiscountAuth($uniacid, 2);
        if($data["status"] == 1) {
            if(empty($card_list)) {
                $card_model = new \app\balancediscount\model\Card();
                $balance_discount_card = $card_model->where(["status" => 1, "uniacid" => $uniacid])->order("discount,id desc")->find();
            } else {
                $card_model = new \app\balancediscount\model\UserCard();
                $balance_discount_card = $card_model->where(["status" => 1, "uniacid" => $uniacid, "user_id" => $user_id])->where("over_time", ">", time())->where("cash", ">", 0)->order("discount,id desc")->find();
            }
            $list["min_discount"] = !empty($balance_discount_card) ? $balance_discount_card->discount : 10;
        }
        if(empty($card_list)) {
            return $list;
        }
        $list["balance_discount_list"] = $card_list;
        foreach ($list["list"] as $k => $v) {
            $total_cash = $v["pay_price"];
            $bin = $v["now_init_service_price"] / ($v["now_init_service_price"] + $v["now_init_material_price"]);
            $list["list"][$k]["balance_discount_cash"] = $v["order_inin_balance_discount_cash"];
            foreach ($card_list as &$value) {
                if(0 < $value["true_total_cash"]) {
                    $set_cash = $value["true_total_cash"] < $total_cash ? $value["true_total_cash"] : $total_cash;
                    $discount_cash = $set_cash * (10 - $value["discount"]) / 10;
                    $list["list"][$k]["balance_discount_list"][] = ["id" => $value["id"], "total_cash" => $set_cash, "discount" => $value["discount"], "discount_cash" => $discount_cash, "value_cash" => round($set_cash - $discount_cash, 2)];
                    $value["true_total_cash"] -= $set_cash;
                    $list["list"][$k]["balance_discount_cash"] += $discount_cash;
                    if(0 < $value["true_total_cash"]) {
                        $service_discount = $list["list"][$k]["balance_discount_cash"] * $bin;
                        $material_discount = round($list["list"][$k]["balance_discount_cash"] - $service_discount, 2);
                        $list["list"][$k]["true_price"] = round($list["list"][$k]["true_price"] - $service_discount, 2);
                        $list["list"][$k]["true_material_price"] = round($list["list"][$k]["true_material_price"] - $material_discount, 2);
                    }
                }
            }
        }
        $list["balance_discount_cash"] = round(array_sum(array_column($card_list, "discount_cash")), 2);
        $list["is_balancediscount"] = 1;
        return $list;
    }
    public function balanceDiscountData($list, $user_id, $service_price, $car_price, $pay_model = 4, $uniacid = 666, $balance_discount_card_arr = [])
    {
        $list["balance_discount_cash"] = 0;
        $data = balanceDiscountAuth($uniacid, 2);
        if($data["auth"] != 1) {
            $list["balance_discount_data"] = $data;
            return $list;
        }
        $balance_card_model = new \app\balancediscount\model\UserCard();
        $card_list = $balance_card_model->getUserCard($user_id, $service_price, $car_price, $balance_discount_card_arr);
        if($data["status"] == 1) {
            if(empty($card_list)) {
                $card_model = new \app\balancediscount\model\Card();
                $balance_discount_card = $card_model->where(["status" => 1, "uniacid" => $uniacid])->order("discount,id desc")->find();
            } else {
                $card_model = new \app\balancediscount\model\UserCard();
                $balance_discount_card = $card_model->where(["status" => 1, "uniacid" => $uniacid, "user_id" => $user_id])->where("over_time", ">", time())->where("cash", ">", 0)->order("discount,id desc")->find();
            }
            $data["min_discount"] = !empty($balance_discount_card) ? $balance_discount_card->discount : 10;
        }
        if(empty($card_list)) {
            $data["user_status"] = 0;
            $list["balance_discount_data"] = $data;
            return $list;
        }
        $data["user_status"] = 1;
        if($pay_model != 4) {
            $list["balance_discount_data"] = $data;
            return $list;
        }
        $data["balance_discount_list"] = $card_list;
        $total_service_discount = $total_material_discount = 0;
        foreach ($list["order_goods"] as $k => $v) {
            $list["order_goods"][$k]["balance_discount_cash"] = 0;
            $total_cash = $v["true_price"] + $v["true_material_price"];
            $bin = 0 < $total_cash ? $v["true_price"] / $total_cash : 0;
            foreach ($card_list as $ks => &$value) {
                if(0 < $value["true_total_cash"]) {
                    $set_cash = $value["true_total_cash"] < $total_cash ? $value["true_total_cash"] : $total_cash;
                    $discount_cash = round($set_cash * (10 - $value["discount"]) / 10, 2);
                    $list["order_goods"][$k]["balance_discount_list"][] = ["id" => $value["id"], "total_cash" => $set_cash, "discount" => $value["discount"], "discount_cash" => $discount_cash, "value_cash" => round($set_cash - $discount_cash, 2)];
                    $value["true_total_cash"] -= $set_cash;
                    $total_cash -= $set_cash;
                    $list["order_goods"][$k]["balance_discount_cash"] += $discount_cash;
                    $service_discount = round($discount_cash * $bin, 2);
                    $material_discount = round($discount_cash - $service_discount, 2);
                    $total_service_discount += $service_discount;
                    $total_material_discount += $material_discount;
                    $list["order_goods"][$k]["true_price"] = round($list["order_goods"][$k]["true_price"] - $service_discount, 2);
                    $list["order_goods"][$k]["true_material_price"] = round($list["order_goods"][$k]["true_material_price"] - $material_discount, 2);
                    if(0 < $value["true_total_cash"]) {
                    }
                }
            }
        }
        $list["balance_discount_cash"] = round(array_sum(array_column($card_list, "discount_cash")), 2);
        $data["is_balancediscount"] = 1;
        $list["balance_discount_data"] = $data;
        $list["goods_price"] = round($list["goods_price"] - $total_service_discount, 2);
        $list["material_price"] = round($list["material_price"] - $total_material_discount, 2);
        return $list;
    }
}

?>